package com.sky.controller.user;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController("UserOrderController")
@RequestMapping("/user/order")
@Slf4j
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 提交订单，去支付
     * @return
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户提交订单：{}",ordersSubmitDTO);
        //用户下单后返回订单数据
        OrderSubmitVO orderInfo = orderService.submit(ordersSubmitDTO);
        return Result.success(orderInfo);
    }

    @PutMapping("/payment")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
        log.info("订单号与支付方式:{}",ordersPaymentDTO);
        LocalDateTime estimateDeliveryTime = orderService.payment(ordersPaymentDTO);
        return Result.success(estimateDeliveryTime);
    }

    @GetMapping("/historyOrders")
    public Result historyOrderPageQuery(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("查询历史订单：{}",ordersPageQueryDTO);
        PageResult pageResult = orderService.pageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("orderDetail/{id}")
    public Result orderDetail(@PathVariable Long id){
        log.info("根据订单id查询订单详情");
        OrderVO orderVO = orderService.getOrderById(id);
        return Result.success(orderVO);
    }

    @PutMapping("cancel/{id}")
    public Result cancel(@PathVariable Long id){
        log.info("取消订单：{}",id);
        OrdersCancelDTO ordersCancelDTO = new OrdersCancelDTO();
        ordersCancelDTO.setId(id);
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id){
        log.info("再来一单：{}",id);
        orderService.repetition(id);
        return Result.success();

    }


    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id){
        log.info("催单");
        orderService.reminder(id);
        return Result.success();

    }

}
